home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_349 / med / source / med200src.zoo / med-objsav.c < prev    next >
C/C++ Source or Header  |  1990-04-08  |  5KB  |  162 lines

  1. /* med-objsav.c: Object file saving routine by Teijo Kinnunen, 1990 */
  2.  
  3. #include "med.h"
  4. #define    ERR return(diskerr(fh,0))
  5. extern struct Kappale far tamakappale;
  6. extern struct Soitin *soitin[];
  7. extern struct Lohko *lohko[];
  8. extern BOOL diskerr(),AskName(),Continue();
  9. extern UWORD lohkoja;
  10. static BPTR fh;
  11.  
  12. static BOOL FWrite(char *ptr,long len)
  13. {
  14.     return((BOOL)(Write(fh,ptr,len) != len));
  15. }
  16.  
  17. static BOOL WriteLong(ULONG lw)
  18. {
  19.     return(FWrite((char *)(&lw),4));
  20. }
  21.  
  22. static ULONG NofLWs(ULONG num)
  23. {
  24.     while(num % 4) num++;
  25.     num /= 4;
  26.     return(num);
  27. }
  28.  
  29. static BOOL WriteName(char *name)
  30. {
  31.     ULONG    namelen = NofLWs(strlen(name));
  32.     UBYTE    padn = ((UBYTE)namelen * 4) - strlen(name),padcnt,zero = 0;
  33.     if(WriteLong(namelen)) return(TRUE);
  34.     if(FWrite(name,strlen(name))) return(TRUE);
  35.     for(padcnt = 0; padcnt < padn; padcnt++) {
  36.         if(FWrite(&zero,1)) return(TRUE);
  37.     }
  38.     return(FALSE);
  39. }
  40.  
  41. static BOOL pad(ULONG size)
  42. {
  43.     ULONG paddedsize = NofLWs(size) * 4;
  44.     UBYTE zero = 0;
  45.     while(size++ < paddedsize) if(FWrite(&zero,1)) return(TRUE);
  46.     return(FALSE);
  47. }
  48.  
  49. BOOL WriteObj(char *name)
  50. {
  51.     char fname2[52];
  52.     struct ObjSong osng;
  53.     ULONG instrsiz = 2,blksiz = 0,bpsiz,ioffstbl[32],blkoffstbl[100] = {0};
  54.     ULONG reloffs = 0;
  55.     UBYTE scnt,nofinstrs = 0,oldexists = 0;
  56.     if(*name == '\0') return(AskName());
  57.     strcpy(fname2,name);
  58.     scnt = strlen(fname2);
  59.     if(scnt > 2 && fname2[scnt - 1] == 'o' && fname2[scnt - 2] == '.');
  60.     else strcat(fname2,".o");
  61.     if(IsHere(fname2)) {
  62.         Ilmoita("File already exists. Continue?");
  63.         oldexists = 1;
  64.         if(!Continue()) return(TRUE);
  65.     }
  66.     if(!(fh = Open2(fname2,MODE_NEWFILE))) return(diskerr(0,0));
  67.     Ilmoita("Saving object file...");
  68. /* -------- First: write hunk_unit, which contains the name of this unit */
  69.     if(WriteLong(0x000003E7)) ERR; /* hunk_unit */
  70.     if(WriteName(fname2)) ERR;
  71.     memcpy((char *)(&osng),(char *)(&tamakappale.soittimenvoimakkuus[0]),
  72.         264);
  73.     osng.blocks = lohkoja;
  74.     osng.songlen = tamakappale.kappaleen_pituus;
  75.     osng.playtransp = tamakappale.playtransp;
  76.     osng.flags = tamakappale.liput;
  77.     osng.slide = tamakappale.vaihtoja;
  78.     memcpy(&osng.midichan[0],&tamakappale.midikanava[0],64);
  79. /* ----------- Write the first hunk, which contains the instruments */
  80.     if(WriteLong(0x400003EA)) ERR; /* hunk_data (chip) */
  81.     for(scnt = 0; scnt < 32; scnt++) {
  82.         if(soitin[scnt]) {
  83.             ioffstbl[scnt] = instrsiz;
  84.             instrsiz += sizeof(struct Soitin);
  85.             instrsiz += (soitin[scnt]->length & 0xfffffffe);
  86.             nofinstrs++;
  87.         } else ioffstbl[scnt] = 0L;
  88.     }
  89.     for(scnt = 0; scnt < lohkoja; scnt++) {
  90.         blkoffstbl[scnt] = blksiz;
  91.         blksiz += 4 + ONETRKSIZE * lohko[scnt]->numtracks;
  92.     }
  93.     bpsiz = 4 * lohkoja; /* size of block pointers */
  94.     if(WriteLong(NofLWs(instrsiz))) ERR;
  95.     if(FWrite((char *)reloffs,2)) ERR; /*write zero to the beginning*/
  96.     for(scnt = 0; scnt < 32; scnt++) {
  97.         if(soitin[scnt]) {
  98.             if(FWrite((char *)soitin[scnt],sizeof(struct Soitin)
  99.                 + (soitin[scnt]->length & 0xfffffffe))) ERR;
  100.         }
  101.     }
  102.     if(pad(instrsiz)) ERR;
  103.     if(WriteLong(0x000003EF)) ERR; /* hunk_ext */
  104.     if(WriteLong(0x01000002)) ERR;
  105.     if(FWrite("_zeroptr",8)) ERR;
  106.     if(WriteLong(0x00000000)) ERR;
  107.     if(WriteLong(0x00000000)) ERR;
  108.     if(WriteLong(0x000003F2)) ERR; /* hunk_end */
  109. /* ---------- Then write the second hunk, music */
  110.     if(WriteLong(0x000003EA)) ERR; /* hunk_data */
  111.     if(WriteLong(NofLWs(blksiz))) ERR;
  112.     for(scnt = 0; scnt < lohkoja; scnt++) {
  113.         if(FWrite((char *)(lohko[scnt]),4 + ONETRKSIZE *
  114.             lohko[scnt]->numtracks)) ERR;
  115.     }
  116.     if(pad(blksiz)) ERR;
  117.     if(WriteLong(0x000003F2)) ERR; /* hunk_end */
  118. /* --------- And finally the third hunk, which contains everythine else */
  119.     if(WriteLong(0x000003EA)) ERR; /* hunk_data */
  120.     /* sizes of: instrument ptrs, song, block ptrs */
  121.     if(WriteLong(NofLWs(32 * 4 + sizeof(osng) + bpsiz))) ERR;
  122.     if(FWrite((char *)ioffstbl,32 * 4)) ERR; /* write the instr ptrs */
  123.     if(FWrite((char *)(&osng),sizeof(osng))) ERR; /* write the song */
  124.     if(FWrite((char *)blkoffstbl,bpsiz)) ERR; /* write the blk ptrs */
  125.     if(pad(32 * 4 + sizeof(osng) + bpsiz)) ERR;
  126. /* --------- We need to relocate something */
  127.     if(WriteLong(0x000003EC)) ERR; /* hunk_reloc32 */
  128.     if(nofinstrs) {
  129.         if(WriteLong((ULONG)nofinstrs)) ERR;
  130.         if(WriteLong(0x00000000)) ERR; /* Instruments in hunk #0 */
  131.         for(scnt = 0; scnt < 32; scnt++, reloffs += 4) {
  132.             if(soitin[scnt]) {
  133.                 if(WriteLong(reloffs)) ERR;
  134.             }
  135.         }
  136.     }
  137.     reloffs = 32 * 4 + sizeof(osng);
  138.     if(WriteLong((ULONG)lohkoja)) ERR;
  139.     if(WriteLong(0x00000001)) ERR; /* Music in hunk #1 */
  140.     for(scnt = 0; scnt < lohkoja; scnt++, reloffs += 4) {
  141.         if(WriteLong(reloffs)) ERR;
  142.     }
  143.     if(WriteLong(0x00000000)) ERR; /* end */
  144. /* -------- And dump the external symbols defined in this file */
  145.     if(WriteLong(0x000003EF)) ERR; /* hunk_ext */
  146.     if(WriteLong(0x01000002)) ERR;
  147.     if(FWrite("_samples",8)) ERR;
  148.     if(WriteLong(0x00000000)) ERR;
  149.     if(WriteLong(0x01000002)) ERR;
  150.     if(FWrite("_song\0\0\0",8)) ERR;
  151.     if(WriteLong(0x00000080)) ERR;
  152.     if(WriteLong(0x01000002)) ERR;
  153.     if(FWrite("_blocks\0",8)) ERR;
  154.     if(WriteLong(0x00000080 + sizeof(osng))) ERR;
  155.     if(WriteLong(0x00000000)) ERR;
  156.     if(WriteLong(0x000003F2)) ERR; /* hunk_end */
  157.     Close(fh); /* huh, huh !! */
  158.     if(!oldexists) InsertSavedFile(fname2);
  159.     Ilmoita("Object file is now saved.");
  160.     return(FALSE);
  161. }
  162.